/**
 * 作者: wgx
 * 日期: 2025-03-25 14:25:51
 * 备注:  用户观看视频激励奖励信息
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.*;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.system.AdvideoMain;
import com.motu.monstercity.server.game.logic.AdvideoManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonObject;

public class UserAdvideo extends UserAdvideoModel {
    public UserAdvideo() {
        super();
    }

    public JsonObject insert() {
        putResetTime(TimeUtils.getCurTime());
        return super.insert();
    }

    public void reset() {
        if (!TimeUtils.isToday(getResetTime())) {
            putDayInfo("");
            putResetTime(TimeUtils.getCurTime());
            update();
        }
    }

    // 获取今日观看次数
    public int takeDayNum(int type, int advideoId) {
        int dayNum = 0;
        if (type == AdvideoManager.ADVIDEO_TYPE_1) {
            String dayInfo = getDayInfo();
            if (Tool.isEmpty(dayInfo)) {
                return 0;
            }
            JsonObject jsonObject = new JsonObject(dayInfo);
            dayNum = jsonObject.getInteger(advideoId + "", 0);
        }
        return dayNum;
    }

    // 更新今日观看广告的次数
    public void addDayNum(int type, int advideoId) {
        String key = advideoId + "";
        JsonObject jsonObject = new JsonObject();

        int dayNum = 0;
        if (type == AdvideoManager.ADVIDEO_TYPE_1) {
            if (!Tool.isEmpty(getDayInfo())) {
                jsonObject = new JsonObject(getDayInfo());
            }
            dayNum = jsonObject.getInteger(key, 0);
            jsonObject.put(key, dayNum + 1);
            putDayInfo(jsonObject.toString());
        }
    }

    // 重置广告观看数据(PS:仅用于不占用每日次数的总数)
    public void resetAdvideoData(PBUserData.Builder pbUserData) {
        putAdCode("");
        putAdStartTime(0);
        update();
        ProtoDataUtils.updatePBUserData(pbUserData, this);
    }
}
